package gov.sat.tax.fwsk.wlrz;



//import gov.sat.tax.fwsk.ycl.TransJmdataPort;
import gov.sat.tax.fwsk.util.IDEA4Java;
import gov.sat.tax.fwsk.util.PORT;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;

public class JmHandler {
	private Logger log = Logger.getLogger(getClass());
	private JmFpDataBean curJmData = null;
	private int status;

	public JmHandler() {
		this.status = 0;
		this.curJmData = new JmFpDataBean();
	}

	public List doJmProc_Jms(String fpmw, String jmip, boolean hzfw) {
		byte[] d = (byte[]) null;
		if (fpmw.length() > 84) {
			d = a2bcd(fpmw);
			byte[] tmp25_21 = d;
			tmp25_21[52] = ((byte) (tmp25_21[52] ^ d[23]));
		}
		log.debug("doJmProc_Jms");
		return doJmProc_Jms_Hx2(fpmw, jmip, hzfw);
	}


	private List doJmProc_Jms_Hx2(String fpmw, String jmip, boolean hzfw) {
		List zhLst = new ArrayList();
		//fpmw = this.fpmw;
		String[] renVal = { "", "", "", "", "", "", "", "", "", "", "" };
		log.debug("doJmProc_Jms_Hx2  "+fpmw+"\n"+jmip);
		byte[] fpmwPre = new byte[100];//发票密文预处理
		byte[] marker = new byte[1];//TODO

		DataInputStream in = null;//调用解密机结果流
		DataOutputStream out = null;//调用解密机参数流
		byte[] indata = new byte[100];//结果缓存区
		byte[] outdata = new byte[97];//参数缓存区

		byte[] mw = (byte[]) null;//密文二进制数组

		StringTokenizer lineunit = null;

		Socket socket = null;
		long ulms = System.currentTimeMillis();

		String chineseAnswer = "";
		List wpmxLst = new ArrayList();

		int ierror = 0;
		String fpmwExt = "";//672位密文
		if (hzfw) {
			fpmwExt = fpmw.substring(108);
			fpmw = fpmw.substring(0, 108);
		}
		log.debug("doJmProc_Jms_Hx2  fpmwPre = tPort.VJMJRZDATAIN(fpmw, 1, fpmwPre, marker);"+fpCheck(fpmw));
		if (!fpCheck(fpmw)) {
			this.curJmData.setErrNo(-1);
			return null;
		}
		log.debug("doJmProc_Jms_Hx2  fpmwPre = tPort.VJMJRZDATAIN(fpmw, 1, fpmwPre, marker);");
		///首先调用本地dll文件，传入108位密文，类型，100密文预处理数组，1位maker标志，返回密文预处理数组。
		PORT tPort = new PORT();
		if (fpmw.length() <= 84) {
			fpmwPre = tPort.VJMJRZDATAIN(fpmw, 1, fpmwPre, marker);
		} else if (hzfw) {
			fpmwPre = tPort.VJMJRZDATAIN(fpmw, 3, fpmwPre, marker);
		} else {
			System.out.println("---------------------------------执行到调用dll ");
			fpmwPre = tPort.VJMJRZDATAIN(fpmw, 2, fpmwPre, marker);
		}

		//获取预处理数组的第82位，作为蔬菜类型
		byte outtype = fpmwPre[82];
		//83位作为错误类型
		ierror = fpmwPre[83];

		if (ierror != 0) {
			this.curJmData.setErrNo(-1);
			return null;
		}

		if (jmip == null) {
			this.status = 530;
			System.out.println("没有可用的解密服务器 !！！");
			return null;
		}
		log.debug("doJmProc_Jms_Hx2  socket = new Socket(jmip, 9999)"+jmip);
		String errno = null;
		try {
			socket = new Socket(jmip, 9999);
			socket.setSoTimeout(5000);
			in = new DataInputStream(socket.getInputStream());
			out = new DataOutputStream(socket.getOutputStream());

			for (int i = 0; i < 10; i++) {
				outdata[i] = 0;
			}
			//参数结构为[0,0,0,0,0,0,0,0,0,0,84,0,0,outtype,密文预处理0-82位,0]一共97位
			outdata[10] = 84;
			outdata[11] = 0;

			outdata[12] = 0;

			outdata[13] = outtype;

			mw = fpmwPre;

			int i = 0;
			for (i = 0; i < 82; i++) {
				outdata[(14 + i)] = mw[i];
			}
			outdata[96] = 0;
			for (i = 0; i < 86; i++) {
				byte[] tmp453_449 = outdata;
				tmp453_449[96] = ((byte) (tmp453_449[96] + outdata[(10 + i)]));
			}

			out.write(outdata);
			out.flush();

			in.read(indata);

			in.close();
			out.close();
			socket.close();
			socket = null;

			//如果第4位为0
			if (indata[3] == 0) {
				if (indata[0] == 29) {
					byte a = 0;
					for (int j = 0; j < 31; j++) {
						a = (byte) (a + indata[j]);
					}

					// indata[31];
				} else if (indata[0] == 36) {
					byte a = 0;
					for (int j = 0; j < 38; j++) {
						a = (byte) (a + indata[j]);
					}

					// TODO indata[38];
				}

				byte[] transferMw = new byte[34];

				if (indata[0] == 29) {
					for (int j = 0; j < 27; j++) {
						transferMw[j] = indata[(j + 4)];
					}

				} else if (indata[0] == 36) {
					for (int j = 0; j < 34; j++) {
						transferMw[j] = indata[(j + 4)];
					}

				}
				log.debug("doJmProc_Jms_Hx2  tPort.VJMJRZDATAOUT(fpmw, 1, transferMw, dataout)"+jmip);
				byte[] dataout = new byte[256];
				// 调用本地dll解密发票信息，参数为108或者84位密文，类型，34或27位机密级返回的密文，256位空字节数组
				if (fpmw.length() <= 84) {
					dataout = tPort.VJMJRZDATAOUT(fpmw, 1, transferMw, dataout);
				} else if (hzfw)
					dataout = tPort.VJMJRZDATAOUT(fpmw, 3, transferMw, dataout);
				else {
					dataout = tPort.VJMJRZDATAOUT(fpmw, 2, transferMw, dataout);
				}

				String tmp = new String(dataout);//为11行（换行）的字符串，包含发票信息和key
				lineunit = new StringTokenizer(tmp);
			} else {
				this.curJmData.setErrNo(-1);
				return null;
			}
			//一些校验
			String tmp;
			if (hzfw) {
				for (i = 0; i < 11; i++) {
					renVal[i] = lineunit.nextToken().trim();
					if (i == 0) {
						errno = renVal[0];
						if ((!errno.equals("0"))
								&& (errno.compareTo("1000") < 0))
							break;
					}
				}
			} else {
				for (i = 0; i < 9; i++) {
					renVal[i] = lineunit.nextToken().trim();

					if (i == 0) {
						errno = renVal[0];
						if ((!errno.equals("0"))
								&& (errno.compareTo("1000") < 0)) {
							break;
						}
					}
				}
			}
			//一些校验
			if (errno == null) {
				this.curJmData.setErrNo(-1);
				return null;
			}
			if (errno.length() == 0) {
				this.curJmData.setErrNo(-1);
				return null;
			}
			int i_errno = Integer.parseInt(errno);
			log.debug("doJmProc_Jms_Hx2  i_errno"+i_errno);
			if ((i_errno == 0) || (i_errno >= 1000)) {
				//
				this.curJmData.setErrNo(i_errno);
				this.curJmData.setFp_dm(renVal[1]);
				this.curJmData.setFp_hm(renVal[2]);
				this.curJmData.setGf_nsrsbh(renVal[4]);
				this.curJmData.setXf_nsrsbh(renVal[5]);
				this.curJmData.setFp_je(Double.parseDouble(renVal[6]));
				this.curJmData.setFp_se(Double.parseDouble(renVal[7]));
				this.curJmData.setKprq(renVal[3]);
				if (renVal[8].indexOf("%") >= 0) {
					renVal[8] = renVal[8].substring(0, renVal[8].length() - 1);
				}
				this.curJmData.setFp_sl(renVal[8]);
				log.debug("doJmProc_Jms_Hx2  hzfw"+ hzfw);
				//如果是汉字防伪
				if (hzfw) {
					chineseAnswer = fpmwExt;//672位密文

					byte[] key = CommonBean.hex2byte(renVal[9]);//第10个为key
					byte[] data = CommonBean.hex2byte(chineseAnswer.substring(
							0, 672));

					IDEA4Java idea = new IDEA4Java();

					//解密调用
					byte[] chinese1 = idea.IdeaEncrypt(key, data, false);
					chinese1 = reverseByte(chinese1);
					byte[] chinese = new byte[336];
					System.arraycopy(chinese1, 0, chinese, 0, 336);
					log.debug("doJmProc_Jms_Hx2  chinese1"+ chinese1);
					byte[] crc_s1 = CommonBean.hex2byte(renVal[10].substring(0,
							48));
					MessageDigest md5 = MessageDigest.getInstance("MD5");
					md5.update(chinese);
					byte[] crc_s2 = md5.digest();
					byte[] crc_source = new byte[crc_s1.length + crc_s2.length];
					System.arraycopy(crc_s1, 0, crc_source, 0, crc_s1.length);
					System.arraycopy(crc_s2, 0, crc_source, crc_s1.length,
							crc_s2.length);
					md5.update(crc_source);
					byte[] crc = md5.digest();
					byte[] crc_target = CommonBean.hex2byte(renVal[10]
							.substring(48));

					byte[] c = a2bcd(fpmw);
					byte[] tmp1520_1516 = c;
					tmp1520_1516[52] = ((byte) (tmp1520_1516[52] ^ c[23]));
					c[52] = ((byte) (c[52] & 0x7F));
					
					/////具体的解析操作///////////////////////////////
					if (c[52] == 66) {
						if ((crc[0] == crc_target[0])
								&& (crc[1] == crc_target[1])) {
							byte flag = chinese[2];
							if (WlrzConst.isDebug()) {
								System.out.println("折行标志：Flag = " + flag);
								System.out.println("解密信息："
										+ new String(chinese));
							}
							if (flag > 0) {
								wpmxLst = getHzInfoList(chinese);
							} else {
								if (WlrzConst.isDebug())
									System.out.println("---进行折行操作处理---");
								int fl = flag < 0 ? flag + 256 : flag;
								String flagByte = Integer.toBinaryString(fl);
								if (WlrzConst.isDebug()) {
									System.out.println("折行标志字节大小：flagByte = "
											+ flagByte);
								}
								String s2 = flagByte.substring(4);
								int hs = Integer.parseInt(s2, 2);
								if (WlrzConst.isDebug())
									System.out.println("折行的行数为：  " + hs);
								String fz = Integer.toBinaryString(chinese[3]);
								if (WlrzConst.isDebug())
									System.out.println("折行清单明细分组情况字符fz：" + fz);
								int wzh = Integer.parseInt(fz, 10);
								if (WlrzConst.isDebug())
									System.out.println("未折行标志：wzh = " + wzh);
								if (wzh != 0) {
									this.curJmData.setIs_Zh("Y");

									int len = fz.length();
									for (int count = 0; count < 7 - len; count++) {
										fz = "0" + fz;
									}

									fz = fz.substring(0,
											fz.lastIndexOf("1") + 1);

									if (fz.startsWith("01")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("01", ",1");
									fz = fz.replaceAll("0", ",0");
									if (fz.startsWith(",")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("0", "");
									String[] mxArray = fz.split(",");
									int count_hs = 0;
									for (int count = 0; count < mxArray.length; count++) {
										int geshu = mxArray[count].length() + 1;
										zhLst.add(new Integer(geshu));
										count_hs += geshu;
									}
									if (hs - count_hs > 0) {
										for (int j = 0; j < hs - count_hs; j++) {
											zhLst.add(new Integer("1"));
										}
									}
									this.curJmData.setZhLst(zhLst);

									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < zhLst.size(); zzz++) {
											System.out.println(zhLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
									wpmxLst = getZhHzInfoList(chinese, hs);
									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < wpmxLst.size(); zzz++) {
											System.out
													.println(wpmxLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
								} else {
									this.curJmData.setIs_Zh("N");
									wpmxLst = getZhHzInfoList(chinese, hs);
								}
							}
						}

					} else if ((c[52] == 67) || (c[52] == 65)) {
						if ((crc[0] != crc_target[0])
								|| (crc[1] != crc_target[1])
								|| (crc[2] != crc_target[2])
								|| (crc[3] != crc_target[3])) {
							System.out.println("汉字信息CRC校验错误！");
						} else {
							byte flag = chinese[2];
							if (WlrzConst.isDebug()) {
								System.out.println("折行标志：Flag = " + flag);
								System.out.println("解密汉字信息："
										+ new String(chinese));
							}
							if (flag > 0) {
								wpmxLst = getHzInfoList(chinese);
							} else {
								if (WlrzConst.isDebug())
									System.out.println("---进行折行操作处理---");
								int fl = flag < 0 ? flag + 256 : flag;
								String flagByte = Integer.toBinaryString(fl);
								if (WlrzConst.isDebug()) {
									System.out.println("折行标志字节大小：flagByte = "
											+ flagByte);
								}
								String s2 = flagByte.substring(4);
								int hs = Integer.parseInt(s2, 2);
								if (WlrzConst.isDebug())
									System.out.println("折行的行数为：  " + hs);
								String fz = Integer.toBinaryString(chinese[3]);
								if (WlrzConst.isDebug())
									System.out.println("折行清单明细分组情况字符fz：" + fz);
								int wzh = Integer.parseInt(fz, 10);
								if (WlrzConst.isDebug())
									System.out.println("未折行标志：wzh = " + wzh);
								if (wzh != 0) {
									this.curJmData.setIs_Zh("Y");

									int len = fz.length();
									for (int count = 0; count < 7 - len; count++) {
										fz = "0" + fz;
									}

									fz = fz.substring(0,
											fz.lastIndexOf("1") + 1);

									if (fz.startsWith("01")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("01", ",1");
									fz = fz.replaceAll("0", ",0");
									if (fz.startsWith(",")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("0", "");

									String[] mxArray = fz.split(",");
									int count_hs = 0;
									for (int count = 0; count < mxArray.length; count++) {
										int geshu = mxArray[count].length() + 1;
										zhLst.add(new Integer(geshu));
										count_hs += geshu;
									}
									if (hs - count_hs > 0) {
										for (int j = 0; j < hs - count_hs; j++) {
											zhLst.add(new Integer("1"));
										}
									}
									this.curJmData.setZhLst(zhLst);

									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < zhLst.size(); zzz++) {
											System.out.println(zhLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
									wpmxLst = getZhHzInfoList(chinese, hs);
									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < wpmxLst.size(); zzz++) {
											System.out
													.println(wpmxLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
								} else {
									this.curJmData.setIs_Zh("N");
									wpmxLst = getZhHzInfoList(chinese, hs);
								}
							}
						}

					}
					/////具体的解析操作///////////////////////////////
				}
				//设置已经取得的汉字信息
				this.curJmData.setHzInfoList(wpmxLst);
			} else if (i_errno == 400) {
				this.curJmData.setErrNo(i_errno);
			} else if (i_errno == 403) {
				this.curJmData.setErrNo(i_errno);
			} else if ((i_errno >= 1) && (i_errno <= 255) && (i_errno >= 1)
					&& (i_errno <= 255)) {
				this.curJmData.setErrNo(i_errno);
			}
		} catch (MalformedURLException e) {
			this.status = 540;
			log.error(e);
			System.out.println(e.getMessage());
		} catch (IOException e) {
			log.error(e);
			this.status = 550;
			System.out.println(e.getMessage());
		} catch (NoSuchAlgorithmException e) {
			log.error(e);
			e.printStackTrace();
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		} finally {
			try {
				if (socket != null)
					socket.close();
				in.close();
				in = null;
			} catch (Exception localException8) {
			}
			ulms = System.currentTimeMillis() - ulms;
			System.out.println("[INFO] 解密完成: " + jmip + " (" + ulms + "ms)\n"+wpmxLst);
		}
		return wpmxLst;
	}

	public List doJmProc(String nsrsbh, String fpmw, String fpid, String jmip,
			boolean hzfw) {
		List zhLst = new ArrayList();

		BufferedReader in = null;
		String[] renVal = { "", "", "", "", "", "", "", "", "", "", "" };
		long ulms = System.currentTimeMillis();

		String qd_bz = hzfw ? "Y" : "N";
		String chineseAnswer = "";
		List wpmxLst = new ArrayList();

		System.out.println("[INFO] JmjIp :" + jmip);

		if (jmip == null) {
			this.status = 530;
			System.out.println("[WARN] 没有可用的解密工作站 !");
			return null;
		}

		System.setProperty("sun.net.client.defaultConnectTimeout", "6000");
		System.setProperty("sun.net.client.defaultReadTimeout", "6000");
		if (hzfw) {
			chineseAnswer = fpmw.substring(108);
			fpmw = fpmw.substring(0, 108);
		}
		String home = "http://" + jmip + ":1299/getfpage?user=" + nsrsbh
				+ "&sycode=" + fpid + "&miwen=" + fpmw + "&FMT=0&MWTYE="
				+ (hzfw ? "3" : fpmw.length() == 84 ? "1" : "2");

		if (WlrzConst.isDebug()) {
			System.out.println("[home] " + home);
		}
		if (WlrzConst.isDebug()) {
			System.out.println("[fpmw] " + fpmw);
		}
		String errno = null;
		try {
			if (!fpCheck(fpmw)) {
				this.curJmData.setErrNo(-1);
				return null;
			}

			URL url = new URL(home);

			URLConnection connection = url.openConnection();
			in = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));

			int i = 0;
			String tmp;
			while ((tmp = in.readLine()) != null) {
				renVal[i] = tmp;

				errno = renVal[0];

				if ((!errno.equals("0")) && (!errno.equals("1001"))
						&& (!errno.equals("1002")))
					break;
				i++;
			}

			in.close();

			if (errno == null) {
				this.curJmData.setErrNo(-1);
				return null;
			}
			if (errno.length() == 0) {
				this.curJmData.setErrNo(-1);
				return null;
			}
			int i_errno = Integer.parseInt(errno);

			switch (i_errno) {
			case 0:
			case 1001:
			case 1002:
				this.curJmData.setErrNo(i_errno);

				this.curJmData.setFp_dm(renVal[1]);

				this.curJmData.setFp_hm(renVal[2]);

				this.curJmData.setGf_nsrsbh(renVal[4]);

				this.curJmData.setXf_nsrsbh(renVal[5]);

				this.curJmData.setFp_je(Double.parseDouble(renVal[6]));

				this.curJmData.setFp_se(Double.parseDouble(renVal[7]));

				this.curJmData.setKprq(renVal[3]);
				if (renVal[8].indexOf("%") >= 0) {
					renVal[8] = renVal[8].substring(0, renVal[8].length() - 1);
				}
				this.curJmData.setFp_sl(renVal[8]);
				if (hzfw) {
					byte[] key = CommonBean.hex2byte(renVal[9]);//获取key
					byte[] data = CommonBean.hex2byte(chineseAnswer.substring(
							0, 672));
					if (WlrzConst.isDebug()) {
						System.out.println("开始汉字解密");
					}
					byte[] chinese1 = new IDEA4Java().IdeaEncrypt(key, data,
							false);
					chinese1 = reverseByte(chinese1);
					byte[] chinese = new byte[336];
					System.arraycopy(chinese1, 0, chinese, 0, 336);
					if (WlrzConst.isDebug())
						System.out.println("汉字解密成功");
					byte[] crc_s1 = CommonBean.hex2byte(renVal[10].substring(0,
							48));
					MessageDigest md5 = MessageDigest.getInstance("MD5");
					md5.update(chinese);
					byte[] crc_s2 = md5.digest();
					byte[] crc_source = new byte[crc_s1.length + crc_s2.length];
					System.arraycopy(crc_s1, 0, crc_source, 0, crc_s1.length);
					System.arraycopy(crc_s2, 0, crc_source, crc_s1.length,
							crc_s2.length);
					md5.update(crc_source);
					byte[] crc = md5.digest();
					byte[] crc_target = CommonBean.hex2byte(renVal[10]
							.substring(48));

					byte[] c = a2bcd(fpmw);
					byte[] tmp1063_1059 = c;
					tmp1063_1059[52] = ((byte) (tmp1063_1059[52] ^ c[23]));
					c[52] = ((byte) (c[52] & 0x7F));
					if (c[52] == 66) {
						if ((crc[0] != crc_target[0])
								|| (crc[1] != crc_target[1])) {
							System.out
									.println("##########HX2/KT2 CRC校验信息开始#############");
							System.out.println("key = " + renVal[9]);
							System.out.println("data = "
									+ chineseAnswer.substring(0, 672));
							System.out
									.println("##########Hx2/KT2 CRC校验信息结束#############");
						} else {
							byte flag = chinese[2];
							if (WlrzConst.isDebug()) {
								System.out.println("折行标志：Flag = " + flag);
								System.out.println("解密汉字信息："
										+ new String(chinese));
							}
							if (flag > 0) {
								wpmxLst = getHzInfoList(chinese);
							} else {
								if (WlrzConst.isDebug())
									System.out.println("---进行折行操作处理---");
								int fl = flag < 0 ? flag + 256 : flag;
								String flagByte = Integer.toBinaryString(fl);
								if (WlrzConst.isDebug()) {
									System.out.println("折行标志字节大小：flagByte = "
											+ flagByte);
								}
								String s2 = flagByte.substring(4);
								int hs = Integer.parseInt(s2, 2);
								if (WlrzConst.isDebug())
									System.out.println("折行的行数为：  " + hs);
								String fz = Integer.toBinaryString(chinese[3]);
								if (WlrzConst.isDebug())
									System.out.println("折行清单明细分组情况字符fz：" + fz);
								int wzh = Integer.parseInt(fz, 10);
								if (WlrzConst.isDebug())
									System.out.println("未折行标志：wzh = " + wzh);
								if (wzh != 0) {
									this.curJmData.setIs_Zh("Y");

									int len = fz.length();
									for (int count = 0; count < 7 - len; count++) {
										fz = "0" + fz;
									}

									fz = fz.substring(0,
											fz.lastIndexOf("1") + 1);

									if (fz.startsWith("01")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("01", ",1");
									fz = fz.replaceAll("0", ",0");
									if (fz.startsWith(",")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("0", "");

									String[] mxArray = fz.split(",");
									int count_hs = 0;
									for (int count = 0; count < mxArray.length; count++) {
										int geshu = mxArray[count].length() + 1;
										zhLst.add(new Integer(geshu));
										count_hs += geshu;
									}
									if (hs - count_hs > 0) {
										for (int j = 0; j < hs - count_hs; j++) {
											zhLst.add(new Integer("1"));
										}
									}
									this.curJmData.setZhLst(zhLst);

									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < zhLst.size(); zzz++) {
											System.out.println(zhLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
									wpmxLst = getZhHzInfoList(chinese, hs);
									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < wpmxLst.size(); zzz++) {
											System.out
													.println(wpmxLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
								} else {
									this.curJmData.setIs_Zh("N");
									wpmxLst = getZhHzInfoList(chinese, hs);
								}
							}
						}
					} else if ((c[52] == 67) || (c[52] == 65)) {
						if ((crc[0] != crc_target[0])
								|| (crc[1] != crc_target[1])
								|| (crc[2] != crc_target[2])
								|| (crc[3] != crc_target[3])) {
							System.out
									.println("##########AI3 CRC校验信息开始#############");
							System.out.println("key = " + renVal[9]);
							System.out.println("data = "
									+ chineseAnswer.substring(0, 672));
							System.out
									.println("##########AI3 CRC校验信息结束#############");
						} else {
							byte flag = chinese[2];
							if (WlrzConst.isDebug()) {
								System.out.println("折行标志：Flag = " + flag);
								System.out.println("解密汉字信息："
										+ new String(chinese));
							}
							if (flag > 0) {
								wpmxLst = getHzInfoList(chinese);
							} else {
								if (WlrzConst.isDebug())
									System.out.println("---进行折行操作处理---");
								int fl = flag < 0 ? flag + 256 : flag;
								String flagByte = Integer.toBinaryString(fl);
								if (WlrzConst.isDebug()) {
									System.out.println("折行标志字节大小：flagByte = "
											+ flagByte);
								}
								String s2 = flagByte.substring(4);
								int hs = Integer.parseInt(s2, 2);
								if (WlrzConst.isDebug())
									System.out.println("折行的行数为：  " + hs);
								String fz = Integer.toBinaryString(chinese[3]);
								if (WlrzConst.isDebug())
									System.out.println("折行清单明细分组情况字符fz：" + fz);
								int wzh = Integer.parseInt(fz, 10);
								if (WlrzConst.isDebug())
									System.out.println("未折行标志：wzh = " + wzh);
								if (wzh != 0) {
									this.curJmData.setIs_Zh("Y");

									int len = fz.length();
									for (int count = 0; count < 7 - len; count++) {
										fz = "0" + fz;
									}

									fz = fz.substring(0,
											fz.lastIndexOf("1") + 1);

									if (fz.startsWith("01")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("01", ",1");
									fz = fz.replaceAll("0", ",0");
									if (fz.startsWith(",")) {
										fz = fz.substring(1, fz.length());
									}

									fz = fz.replaceAll("0", "");

									String[] mxArray = fz.split(",");
									int count_hs = 0;
									for (int count = 0; count < mxArray.length; count++) {
										int geshu = mxArray[count].length() + 1;
										zhLst.add(new Integer(geshu));
										count_hs += geshu;
									}

									if (hs - count_hs > 0) {
										for (int j = 0; j < hs - count_hs; j++) {
											zhLst.add(new Integer("1"));
										}
									}
									this.curJmData.setZhLst(zhLst);

									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < zhLst.size(); zzz++) {
											System.out.println(zhLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
									wpmxLst = getZhHzInfoList(chinese, hs);
									if (WlrzConst.isDebug()) {
										System.out
												.println("////////////////////////////");
										for (int zzz = 0; zzz < wpmxLst.size(); zzz++) {
											System.out
													.println(wpmxLst.get(zzz));
										}
										System.out
												.println("////////////////////////////");
									}
								} else {
									this.curJmData.setIs_Zh("N");
									wpmxLst = getZhHzInfoList(chinese, hs);
								}
							}
						}
					}
				}
				this.curJmData.setHzInfoList(wpmxLst);
				this.curJmData.setQd_bz(qd_bz);
				break;
			case 400:
				this.curJmData.setErrNo(i_errno);
				break;
			case 403:
				this.curJmData.setErrNo(i_errno);
				break;
			default:
				if ((i_errno >= 1) && (i_errno <= 255))
					this.curJmData.setErrNo(i_errno);
				break;
			}
		} catch (MalformedURLException e) {
			this.status = 540;
			System.out.println(e.getMessage());
		} catch (ConnectException e) {
			this.status = 540;
			System.out.println("[info] 解密机连接超时 " + e.getMessage());
		} catch (IOException e) {
			this.status = 550;
			System.out.println(e.getMessage());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ulms = System.currentTimeMillis() - ulms;
			System.out.println("[INFO] 解密完成: " + jmip + " (" + ulms + "ms)");
		}
		return wpmxLst;
	}
	static String fpmw ="22+828453*630<-<+-+--70-*300><34722>>42><53-420-*7-9*79><+/+5--**5*>>75<2183+6-76+*312<>748//00-2><53-42496>9DBC4C669119FC60B0A0257B97BFD5B5B6A630CC70BE096924F2A8156A700C6E9ECF5F537CE0A2010EEA949AAA3BF42796A184E40E29E3FC3B5460A938D73560E0CBF0F98401B25DA67EA71569F707CBF0E945F4F2824BCB7CCA121B485F04E8CDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0BCDE09EB4F8768B0B";
	static String fpid = "";
	static String jmip = "83.16.18.155";
	static boolean hzfw =true;
	public static void main(String[] args){
		new JmHandler().doJmProc_Jms(fpmw, jmip, hzfw);//.get678Content("nsrsbh", fpmw, fpid, jmip, hzfw);
	}
	
 
	
	public JmFpDataBean getCurJmData() {
		return this.curJmData;
	}

	public int getStatus() {
		return this.status;
	}

	public void setCurJmData(JmFpDataBean newCurJmData) {
		this.curJmData = newCurJmData;
	}

	public void setStatus(int newStatus) {
		this.status = newStatus;
	}

	private List getHzInfoList(byte[] data) {
		if (data.length < 6) {
			return null;
		}

		int chineseStartPos = 3 + data[2] * 3;
		int[] len = new int[chineseStartPos];
		for (int i = 0; i < chineseStartPos; i++) {
			len[i] = data[i];
		}
		List rtn = new ArrayList(chineseStartPos);
		int offset = chineseStartPos;
		rtn.add(replaceInvlidCharacter(new String(data, offset, len[0])));
		offset += len[0];
		rtn.add(replaceInvlidCharacter(new String(data, offset, len[1])));
		offset += len[1];
		rtn.add(String.valueOf(len[2]));

		for (int k = 3; k < chineseStartPos; k++) {
			if (len[k] > 0) {
				rtn.add(replaceInvlidCharacter(new String(data, offset, len[k])));
				offset += len[k];
			} else {
				rtn.add("");
			}
		}
		return rtn;
	}

	private byte[] reverseByte(byte[] input) {
		for (int i = 0; i < input.length; i++) {
			if (i % 2 == 0) {
				byte temp = input[i];
				input[i] = input[(i + 1)];
				input[(i + 1)] = temp;
			}
		}

		return input;
	}

	public boolean fpCheck(String fpmw) {
		boolean checkResult = false;
		if (fpmw.length() == 84)
			checkResult = fpCheckSum(fpmw);
		else if (fpmw.length() == 108)
			checkResult = fpCheckSum108(fpmw);
		else {
			checkResult = false;
		}
		return checkResult;
	}

	public boolean fpCheckSum(String fpmw) {
		int wCheckSum = 0;
		int wCorrect = 0;

		for (int i = 0; i < 84; i++) {
			char s = fpmw.charAt(i);
			int wValue = GetByteValue(s);

			if (wValue == -1)
				return false;
			if (i % 2 == 0)
				wValue <<= 4;
			if (i < 82)
				wCheckSum += wValue;
			else
				wCorrect += wValue;
		}
		wCheckSum &= 255;
		return wCheckSum == wCorrect;
	}

	public boolean fpCheckSum108(String fpmw) {
		int wCheckSum = 0;
		int wCorrect = 0;

		for (int i = 0; i < 108; i++) {
			char s = fpmw.charAt(i);
			int wValue = GetByteValue(s);

			if (wValue == -1)
				return false;
			if (i % 2 == 0)
				wValue <<= 4;
			if (i < 106)
				wCheckSum += wValue;
			else
				wCorrect += wValue;
		}
		wCheckSum &= 255;
		return wCheckSum == wCorrect;
	}

	int GetByteValue(char bChar) {
		if ((bChar >= '0') && (bChar <= '9')) {
			return bChar - '0';
		}
		switch (bChar) {
		case '+':
			return 10;
		case '-':
			return 11;
		case '*':
			return 12;
		case '/':
			return 13;
		case '<':
			return 14;
		case '>':
			return 15;
		}
		return -1;
	}

	private String replaceInvlidCharacter(String original) {
		String newString = original.replaceAll("\t", "");
		newString = newString.replaceAll("\t", "");
		newString = newString.replaceAll(" ", "");
		newString = newString.replaceAll("　", "");

		return newString;
	}

	public static byte[] a2bcd(String hex) {
		int len = hex.length() / 2;
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < achar.length; i += 2) {
			char a = achar[i];
			char a_1 = achar[(i + 1)];
			a = mw2a(a);
			a_1 = mw2a(a_1);

			result[(i / 2)] = ((byte) (toByte(a) << 4 | toByte(a_1)));
		}
		return result;
	}

	public static char mw2a(char a) {
		if ((a < '0') || (a > '9')) {
			a = (char) (65 + "+-*/<>".indexOf(a));
		}
		return a;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}

	private List getZhHzInfoList(byte[] data, int hs) {
		if (data.length < 7) {
			return null;
		}

		int chineseStartPos = 4 + hs * 3;

		int[] len = new int[chineseStartPos];
		for (int i = 0; i < chineseStartPos; i++) {
			len[i] = data[i];
		}

		List rtn = new ArrayList(chineseStartPos);
		int offset = chineseStartPos;
		rtn.add(new String(data, offset, len[0]));
		offset += len[0];
		rtn.add(new String(data, offset, len[1]));
		offset += len[1];

		rtn.add(String.valueOf(hs));

		for (int k = 4; k < chineseStartPos; k++) {
			if (len[k] > 0) {
				rtn.add(new String(data, offset, len[k]));
				offset += len[k];
			} else {
				rtn.add("");
			}
		}

		return rtn;
	}
}